########
# Usage: Rscript searchResult_mean_linePoint.R 'longRNA' "TRAF3,GAB2,SNX1,ID1,RALY,DYNLRB1,MCL1"
#        Rscript searchResult_mean_linePoint.R 'circRNA' "exo_circ_000452,exo_circ_000292,exo_circ_001535,exo_circ_001573,exo_circ_004802,exo_circ_039654,exo_circ_001363,exo_circ_044529,exo_circ_003784,exo_circ_000855,exo_circ_024448"
#        Rscript searchResult_mean_linePoint.R 'cell' "Adipose Tissue,CD8_naive"
#        Rscript searchResult_mean_linePoint.R 'pathway' "pathway00001,pathway00465,pathway01805,pathway03023,pathway06573,pathway09936"               
# 传入参数： targetType = 'longRNA'
#           targets = c("TRAF3", "GAB2", "SNX1","ID1","RALY","DYNLRB1","MCL1")
#
########
library(reshape2)
library(ggplot2)
library(RColorBrewer)

library(optparse)

option_list <- list(
  make_option("--d", default = "", type = "character", help = "data file"),
  make_option("--targetType", default = "", type = "character", help = "target type"),
  make_option("--t", default = "", type = "character", help = "targets"),
  make_option("--o", default = "", type = "character", help = "output png file")
)
opt <- parse_args(OptionParser(option_list = option_list))

Args = commandArgs()
targetType = opt$targetType
targets = opt$t
targets = as.vector(unlist(strsplit(targets, ',')))
print(targetType)
print(targets)

# path = "D:\\exo\\905\\submit 6.1"
# setwd(path)
# 
# targetType = 'longRNA'
# targets = c("GDF5","MMP24","NECAB3","ID1","RALY","DYNLRB1","C20orf173","NOP53","MCL1","GPSM3")
# 
# targetType = 'circRNA'
# targets = c("exo_circ_000452","exo_circ_000292","exo_circ_001535","exo_circ_001573","exo_circ_004802","exo_circ_039654","exo_circ_001363","exo_circ_044529","exo_circ_003784","exo_circ_000855","exo_circ_024448")
# 
# targetType = 'cell'
# targets = c("Adipose Tissue", "CD8_naive")
# 
# targetType = 'pathway'
# targets = c('P00001', 'P00465', 'P01805', 'P03023', 'P06573', 'P09936')


TargetNumber = length(targets)
w=12
h=6
outfile =opt$o
datafile = opt$d
data = read.csv(datafile, sep='\t', header = TRUE, row.names=1)
data.selected = data[targets, ]
data.selected$Target = row.names(data.selected)
data_m = melt(data.selected, id.vars=c("Target"))
cohortOrder = c("Urine","CSF","Bile","Healthy","Benign","BRCA","CHD","CRC","ESCC","GBM","GC","HCC","KIRC","ML","MEL","OV","PAAD","SCLC")
data_m$variable = factor(data_m$variable, levels = cohortOrder)

cols.time = ceiling(TargetNumber/10)
shapes.time = 2 * ceiling(TargetNumber/10)
cols = c(brewer.pal(7,'Set2'), brewer.pal(3,'Pastel1'))
cols = rep(cols, cols.time)
shapes = c(15,16,17,18,19)
shapes = rep(shapes, shapes.time)

pdf(outfile,  width=w, height=h)
p = ggplot(data=data_m,mapping=aes(x=variable, y=value, group=Target))+
  geom_line(aes(color=Target), size=1) +
  geom_point(aes(color=Target, shape=Target), size=3) +
  scale_shape_manual(values=shapes)+
  scale_color_manual(values=cols)+
  theme_bw()+ #scale_y_continuous(expand = c(0, 0))+
  xlab('') +
  theme(legend.position="left", legend.title=element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid= element_line(colour = 'gray'),
        panel.border = element_rect(linetype = 'solid', size = 1.2,fill = NA),
        axis.text.y = element_text(face = 'bold',color = 'black',size = 10, angle = 360),
        axis.text.x = element_text(face = 'bold',color = 'black',size = 10, angle = 45, hjust = 1),
        axis.title = element_text(size = 14, face = "bold"))
if (targetType == 'longRNA'){
  p = p + ylab('TPM')
}else if(targetType == 'circRNA'){
  p = p + ylab('CPM')
}else if(targetType == 'pathway'){
  p = p + ylab('ssGSEA score')
}else{
  p = p + ylab('Absolute abundance')
}

print(p)
dev.off()

